<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/css/all.min.css" integrity="sha256-AbA177XfpSnFEvgpYu1jMygiLabzPCJCRIBtR5jGc0k=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"tomstand.gitee.io","root":"/","images":"/images","scheme":"Muse","darkmode":false,"version":"8.13.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":false,"style":null},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"Searching...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"}}</script><script src="/js/config.js"></script>

    <meta name="description" content="Python爬取网站图片数据找到需要爬取的网站地址 模拟网站http请求根据调试模式获取的了解读取到真实的地址url，读取请求头数据和参数信息，模拟http请求调用 1234567891011121314151617181920212223242526272829303132333435363738394041424344import requestsfrom urllib.parse impor">
<meta property="og:type" content="article">
<meta property="og:title" content="猿日记">
<meta property="og:url" content="https://tomstand.gitee.io/2023/11/21/Python%E7%88%AC%E5%8F%96%E7%BD%91%E7%AB%99%E5%9B%BE%E7%89%87%E6%95%B0%E6%8D%AE/index.html">
<meta property="og:site_name" content="猿日记">
<meta property="og:description" content="Python爬取网站图片数据找到需要爬取的网站地址 模拟网站http请求根据调试模式获取的了解读取到真实的地址url，读取请求头数据和参数信息，模拟http请求调用 1234567891011121314151617181920212223242526272829303132333435363738394041424344import requestsfrom urllib.parse impor">
<meta property="og:locale" content="en_US">
<meta property="og:image" content="https://img-blog.csdnimg.cn/img_convert/19f4e90034623eeebfb54ce4223b8d5b.png">
<meta property="og:image" content="https://img-blog.csdnimg.cn/img_convert/3c4f87f370c7756d6117a609b4a53109.png">
<meta property="article:published_time" content="2023-11-21T12:16:19.928Z">
<meta property="article:modified_time" content="2023-11-21T12:16:19.928Z">
<meta property="article:author" content="tomstand">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://img-blog.csdnimg.cn/img_convert/19f4e90034623eeebfb54ce4223b8d5b.png">


<link rel="canonical" href="https://tomstand.gitee.io/2023/11/21/Python%E7%88%AC%E5%8F%96%E7%BD%91%E7%AB%99%E5%9B%BE%E7%89%87%E6%95%B0%E6%8D%AE/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"en","comments":true,"permalink":"https://tomstand.gitee.io/2023/11/21/Python%E7%88%AC%E5%8F%96%E7%BD%91%E7%AB%99%E5%9B%BE%E7%89%87%E6%95%B0%E6%8D%AE/","path":"2023/11/21/Python爬取网站图片数据/","title":""}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title> | 猿日记</title>
  






  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar" role="button">
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">猿日记</p>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>







</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#Python%E7%88%AC%E5%8F%96%E7%BD%91%E7%AB%99%E5%9B%BE%E7%89%87%E6%95%B0%E6%8D%AE"><span class="nav-number">1.</span> <span class="nav-text">Python爬取网站图片数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%89%BE%E5%88%B0%E9%9C%80%E8%A6%81%E7%88%AC%E5%8F%96%E7%9A%84%E7%BD%91%E7%AB%99%E5%9C%B0%E5%9D%80"><span class="nav-number">2.</span> <span class="nav-text">找到需要爬取的网站地址</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%A8%A1%E6%8B%9F%E7%BD%91%E7%AB%99http%E8%AF%B7%E6%B1%82"><span class="nav-number">3.</span> <span class="nav-text">模拟网站http请求</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%A7%A3%E6%9E%90%E8%BF%94%E5%9B%9E%E5%8F%82%E6%95%B0%E4%B8%AD%E7%9A%84%E5%9B%BE%E7%89%87%E4%BF%A1%E6%81%AF"><span class="nav-number">4.</span> <span class="nav-text">解析返回参数中的图片信息</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%B8%8B%E8%BD%BD%E5%9B%BE%E7%89%87%E4%BF%A1%E6%81%AF%E5%88%B0%E6%9C%AC%E5%9C%B0"><span class="nav-number">5.</span> <span class="nav-text">下载图片信息到本地</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%B0%83%E7%94%A8%E4%B8%BB%E6%96%B9%E6%B3%95%E6%89%A7%E8%A1%8C%E5%BC%80%E5%A7%8B%E6%95%B0%E6%8D%AE%E7%88%AC%E5%8F%96"><span class="nav-number">6.</span> <span class="nav-text">调用主方法执行开始数据爬取</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">tomstand</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">12</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="Back to top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="en">
    <link itemprop="mainEntityOfPage" href="https://tomstand.gitee.io/2023/11/21/Python%E7%88%AC%E5%8F%96%E7%BD%91%E7%AB%99%E5%9B%BE%E7%89%87%E6%95%B0%E6%8D%AE/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="tomstand">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="猿日记">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content=" | 猿日记">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2023-11-21 20:16:19" itemprop="dateCreated datePublished" datetime="2023-11-21T20:16:19+08:00">2023-11-21</time>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h3 id="Python爬取网站图片数据"><a href="#Python爬取网站图片数据" class="headerlink" title="Python爬取网站图片数据"></a>Python爬取网站图片数据</h3><h3 id="找到需要爬取的网站地址"><a href="#找到需要爬取的网站地址" class="headerlink" title="找到需要爬取的网站地址"></a>找到需要爬取的网站地址</h3><p><img src="https://img-blog.csdnimg.cn/img_convert/19f4e90034623eeebfb54ce4223b8d5b.png" alt="image-20201229150424266"></p>
<h3 id="模拟网站http请求"><a href="#模拟网站http请求" class="headerlink" title="模拟网站http请求"></a>模拟网站http请求</h3><p>根据调试模式获取的了解读取到真实的地址url，读取请求头数据和参数信息，模拟http请求调用</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="keyword">from</span> urllib.parse <span class="keyword">import</span> urlencode</span><br><span class="line"><span class="comment"># requests内置的状态码，可判断url响应是否正确</span></span><br><span class="line"><span class="keyword">from</span> requests <span class="keyword">import</span> codes</span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="keyword">from</span> hashlib <span class="keyword">import</span> md5</span><br><span class="line"><span class="keyword">from</span> multiprocessing.pool <span class="keyword">import</span> Pool</span><br><span class="line"><span class="keyword">import</span> re</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">get_page</span>(<span class="params">offset</span>):</span></span><br><span class="line">    <span class="comment"># 必须的头请求头信息</span></span><br><span class="line">    headers = &#123;</span><br><span class="line">        <span class="string">&#x27;cookie&#x27;</span>: <span class="string">&#x27;tt_webid=6667396596445660679; csrftoken=3a212e0c06e7821650315a4fecf47ac9; tt_webid=6667396596445660679; WEATHER_CITY=%E5%8C%97%E4%BA%AC; UM_distinctid=16b846003e03d7-0dd00a2eb5ea11-353166-1fa400-16b846003e1566; CNZZDATA1259612802=2077267981-1561291030-https%253A%252F%252Fwww.baidu.com%252F%7C1561361230; __tasessionId=4vm71cznd1561363013083; sso_uid_tt=47d6f9788277e4e071f3825a3c36a294; toutiao_sso_user=e02fd616c83dff880adda691cd201aaa; login_flag=6859a0b8ffdb01687b00fe96bbeeba6e; sessionid=21f852358a845d783bdbe1236c9b385b; uid_tt=d40499ec45187c2d411cb7bf656330730d8c15a783bb6284da0f73104cd300a2; sid_tt=21f852358a845d783bdbe1236c9b385b; sid_guard=&quot;21f852358a845d783bdbe1236c9b385b|1561363028|15552000|Sat\054 21-Dec-2019 07:57:08 GMT&quot;; s_v_web_id=6f40e192e0bdeb62ff50fca2bcdf2944&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;user-agent&#x27;</span>: <span class="string">&#x27;Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.157 Safari/537.36&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;x-requested-with&#x27;</span>: <span class="string">&#x27;XMLHttpRequest&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;referer&#x27;</span>: <span class="string">&#x27;https://www.toutiao.com/search/?keyword=%E8%A1%97%E6%8B%8D&#x27;</span>,</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment"># 获得的真是url必须的参数构造</span></span><br><span class="line">    params = &#123;</span><br><span class="line">        <span class="string">&#x27;aid&#x27;</span>: <span class="string">&#x27;24&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;app_name&#x27;</span>: <span class="string">&#x27;web_search&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;offset&#x27;</span>: offset,</span><br><span class="line">        <span class="string">&#x27;format&#x27;</span>: <span class="string">&#x27;json&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;keyword&#x27;</span>: <span class="string">&#x27;街拍&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;autoload&#x27;</span>: <span class="string">&#x27;true&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;count&#x27;</span>: <span class="string">&#x27;20&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;en_qc&#x27;</span>: <span class="string">&#x27;1&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;cur_tab&#x27;</span>: <span class="string">&#x27;1&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;from&#x27;</span>: <span class="string">&#x27;search_tab&#x27;</span>,</span><br><span class="line">        <span class="string">&#x27;pd&#x27;</span>: <span class="string">&#x27;synthesis&#x27;</span>,</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment"># 真实url的起始url</span></span><br><span class="line">    base_url = <span class="string">&#x27;https://www.toutiao.com/api/search/content/?&#x27;</span></span><br><span class="line">    <span class="comment"># python内置库解码后的真实url</span></span><br><span class="line">    url = base_url + urlencode(params)</span><br><span class="line">    <span class="keyword">try</span>:</span><br><span class="line">        resp = requests.get(url, headers=headers)</span><br><span class="line">        <span class="comment"># 是正确的响应，即200</span></span><br><span class="line">        <span class="keyword">if</span> <span class="number">200</span> == resp.status_code:</span><br><span class="line">            <span class="keyword">return</span> resp.json()</span><br><span class="line">    <span class="keyword">except</span> requests.ConnectionError:</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">None</span></span><br></pre></td></tr></table></figure>

<h3 id="解析返回参数中的图片信息"><a href="#解析返回参数中的图片信息" class="headerlink" title="解析返回参数中的图片信息"></a>解析返回参数中的图片信息</h3><p><img src="https://img-blog.csdnimg.cn/img_convert/3c4f87f370c7756d6117a609b4a53109.png" alt="image-20201229150950697"></p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">get_images</span>(<span class="params">json</span>):</span></span><br><span class="line">    <span class="comment"># 街拍的总数据</span></span><br><span class="line">    <span class="keyword">if</span> json.get(<span class="string">&#x27;data&#x27;</span>):</span><br><span class="line">        data = json.get(<span class="string">&#x27;data&#x27;</span>)</span><br><span class="line">        <span class="keyword">for</span> item <span class="keyword">in</span> data:</span><br><span class="line">            <span class="keyword">if</span> item.get(<span class="string">&#x27;title&#x27;</span>) <span class="keyword">is</span> <span class="literal">None</span>:</span><br><span class="line">                <span class="keyword">continue</span></span><br><span class="line">            title = re.sub(<span class="string">&#x27;[\t]&#x27;</span>, <span class="string">&#x27;&#x27;</span>, item.get(<span class="string">&#x27;title&#x27;</span>))</span><br><span class="line">            <span class="built_in">print</span>(item.get(<span class="string">&#x27;title&#x27;</span>))</span><br><span class="line">            images = item.get(<span class="string">&#x27;image_list&#x27;</span>)</span><br><span class="line">            <span class="keyword">if</span> images <span class="keyword">is</span> <span class="literal">None</span>:</span><br><span class="line">                <span class="keyword">continue</span></span><br><span class="line">            <span class="keyword">for</span> image <span class="keyword">in</span> images:</span><br><span class="line">                <span class="comment"># 修复了url更改导致了爬取到的部分是小图的问题</span></span><br><span class="line">                <span class="keyword">for</span> each <span class="keyword">in</span> image.get(<span class="string">&#x27;url&#x27;</span>):</span><br><span class="line">                    <span class="keyword">if</span> each == <span class="string">&#x27;x&#x27;</span>:</span><br><span class="line">                        origin_image = re.sub(<span class="string">&quot;list/190x124&quot;</span>, <span class="string">&quot;large&quot;</span>, image.get(<span class="string">&#x27;url&#x27;</span>))</span><br><span class="line">                        <span class="keyword">break</span></span><br><span class="line">                    <span class="keyword">else</span>:</span><br><span class="line">                        origin_image = re.sub(<span class="string">&quot;list&quot;</span>, <span class="string">&quot;large&quot;</span>, image.get(<span class="string">&#x27;url&#x27;</span>))</span><br><span class="line">                <span class="keyword">yield</span> &#123;</span><br><span class="line">                    <span class="string">&#x27;image&#x27;</span>: origin_image,</span><br><span class="line">                    <span class="string">&#x27;title&#x27;</span>: title</span><br><span class="line">                &#125;</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h3 id="下载图片信息到本地"><a href="#下载图片信息到本地" class="headerlink" title="下载图片信息到本地"></a>下载图片信息到本地</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">save_image</span>(<span class="params">item</span>):</span></span><br><span class="line">    <span class="comment"># 文件保存的相对路径</span></span><br><span class="line">    <span class="comment"># img_path = &#x27;Jie_Pai&#x27; + os.path.sep + item.get(&#x27;title&#x27;)</span></span><br><span class="line">    img_path = <span class="string">&#x27;Jie_Pai&#x27;</span></span><br><span class="line">    <span class="built_in">print</span>(img_path)</span><br><span class="line">    <span class="keyword">if</span> <span class="keyword">not</span> os.path.exists(img_path):</span><br><span class="line">        os.makedirs(img_path)</span><br><span class="line">    <span class="keyword">try</span>:</span><br><span class="line">        <span class="comment"># 下载真实图片</span></span><br><span class="line">        resp = requests.get(item.get(<span class="string">&#x27;image&#x27;</span>))</span><br><span class="line">        <span class="comment"># 内置状态码，判断响应是否正确</span></span><br><span class="line">        <span class="keyword">if</span> codes.ok == resp.status_code:</span><br><span class="line">            file_path = img_path + os.path.sep + <span class="string">&#x27;&#123;file_name&#125;.&#123;file_suffix&#125;&#x27;</span>.<span class="built_in">format</span>(</span><br><span class="line">                <span class="comment"># 图片内容的md5值，避免重复</span></span><br><span class="line">                file_name=md5(resp.content).hexdigest(),</span><br><span class="line">                file_suffix=<span class="string">&#x27;jpg&#x27;</span>)</span><br><span class="line">            <span class="keyword">if</span> <span class="keyword">not</span> os.path.exists(file_path):</span><br><span class="line">                <span class="keyword">with</span> <span class="built_in">open</span>(file_path, <span class="string">&#x27;wb&#x27;</span>) <span class="keyword">as</span> f:</span><br><span class="line">                    f.write(resp.content)</span><br><span class="line">                <span class="built_in">print</span>(<span class="string">&#x27;Downloaded image path is %s&#x27;</span> % file_path)</span><br><span class="line">            <span class="keyword">else</span>:</span><br><span class="line">                <span class="built_in">print</span>(<span class="string">&#x27;Already Downloaded&#x27;</span>, file_path)</span><br><span class="line">    <span class="comment"># 报错</span></span><br><span class="line">    <span class="keyword">except</span> Exception <span class="keyword">as</span> e:</span><br><span class="line">        <span class="built_in">print</span>(e)</span><br></pre></td></tr></table></figure>

<h3 id="调用主方法执行开始数据爬取"><a href="#调用主方法执行开始数据爬取" class="headerlink" title="调用主方法执行开始数据爬取"></a>调用主方法执行开始数据爬取</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">main</span>(<span class="params">offset</span>):</span></span><br><span class="line">    <span class="comment"># 以什么倍数来增长</span></span><br><span class="line">    json = get_page(offset)</span><br><span class="line">    <span class="keyword">for</span> item <span class="keyword">in</span> get_images(json):</span><br><span class="line">        save_image(item)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">GROUP_START = <span class="number">0</span></span><br><span class="line">GROUP_END = <span class="number">7</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">&#x27;__main__&#x27;</span>:</span><br><span class="line">    pool = Pool()</span><br><span class="line">    groups = ([x * <span class="number">20</span> <span class="keyword">for</span> x <span class="keyword">in</span> <span class="built_in">range</span>(GROUP_START, GROUP_END + <span class="number">1</span>)])</span><br><span class="line">    pool.<span class="built_in">map</span>(main, groups)</span><br><span class="line">    pool.close()</span><br><span class="line">    pool.join()</span><br></pre></td></tr></table></figure>


    </div>

    
    
    

    <footer class="post-footer">

        

          <div class="post-nav">
            <div class="post-nav-item">
            </div>
            <div class="post-nav-item">
                <a href="/2023/11/21/%E4%BA%92%E8%81%94%E7%BD%91%E5%85%AC%E5%8F%B8%E5%9F%BA%E6%9C%AC%E7%9A%84%E6%8A%80%E6%9C%AF%E6%A0%88/" rel="next" title="">
                   <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">tomstand</span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a>
  </div>

    </div>
  </footer>

  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  





  





</body>
</html>
